--[[
	重写科技树
	时光鸡@2018年3月1日20:08:59
	思路是先把关系按照链表的方式连起来，然后hash
]]
local M = {}
local techTree = {
	npc_dota_hero_nevermore = {
		youxia_tower5 = {"youxia1_3"},
		youxia_tower6 = {"youxia2_3"},
		youxia_tower7 = {"youxia3_3"},
		youxia_tower8 = {"youxia4_3"},
		youxia_tower9 = {"youxia5_3"},
		youxia_tower10 = {"youxia6_3"},
		youxia_tower11 = {"youxia9_3"},
		youxia7_3 = {"youxia11"},
		youxia7_4 = {"youxia11"},
		youxia8_3 = {"youxia11"},
		youxia8_4 = {"youxia11"},
		youxia10_3 = {"youxia11"},
		youxia10_4 = {"youxia11"},
	},
	npc_dota_hero_wisp = {
		mojianshi_tower5 = {"mojianshi1_3"},
		mojianshi_tower6 = {"mojianshi2_3"},
		mojianshi_tower7 = {"mojianshi3_3"},
		mojianshi_tower8 = {"mojianshi4_3"},
		mojianshi_tower9 = {"mojianshi5_3"},
		mojianshi_tower10 = {"mojianshi6_3"},
		mojianshi_tower11 = {"mojianshi9_3"},
		mojianshi7_3 = {"mojianshi11"},
		mojianshi7_4 = {"mojianshi11"},
		mojianshi8_3 = {"mojianshi11"},
		mojianshi8_4 = {"mojianshi11"},
		mojianshi10_3 = {"mojianshi11"},
		mojianshi10_4 = {"mojianshi11"},
	},
	npc_dota_hero_doom_bringer = {
		yemanren_tower5 = {"yemanren1_3"},
		yemanren_tower6 = {"yemanren2_3"},
		yemanren_tower7 = {"yemanren3_3"},
		yemanren_tower8 = {"yemanren4_3"},
		yemanren_tower9 = {"yemanren5_3"},
		yemanren_tower10 = {"yemanren6_3"},
		yemanren_tower11 = {"yemanren9_3"},
		yemanren7_3 = {"yemanren11"},
		yemanren7_4 = {"yemanren11"},
		yemanren8_3 = {"yemanren11"},
		yemanren8_4 = {"yemanren11"},
		yemanren10_3 = {"yemanren11"},
		yemanren10_4 = {"yemanren11"},
	},
	npc_dota_hero_beastmaster = {
		lieren_tower5 = {"lieren1_3"},
		lieren_tower6 = {"lieren2_3"},
		lieren_tower7 = {"lieren3_3"},
		lieren_tower8 = {"lieren4_3"},
		lieren_tower9 = {"lieren5_3"},
		lieren_tower10 = {"lieren6_3"},
		lieren_tower11 = {"lieren9_3"},
		lieren7_3 = {"lieren11"},
		lieren7_4 = {"lieren11"},
		lieren8_3 = {"lieren11"},
		lieren8_4 = {"lieren11"},
		lieren10_3 = {"lieren11"},
		lieren10_4 = {"lieren11"},
	},
	npc_dota_hero_rubick = {
		xiaotou_tower5 = {"xiaotou1_3"},
		xiaotou_tower6 = {"xiaotou2_3"},
		xiaotou_tower7 = {"xiaotou3_3"},
		xiaotou_tower8 = {"xiaotou4_3"},
		xiaotou_tower9 = {"xiaotou5_3"},
		xiaotou_tower10 = {"xiaotou6_3"},
		xiaotou_tower11 = {"xiaotou9_3"},
		xiaotou7_3 = {"xiaotou11"},
		xiaotou7_4 = {"xiaotou11"},
		xiaotou8_3 = {"xiaotou11"},
		xiaotou8_4 = {"xiaotou11"},
		xiaotou10_3 = {"xiaotou11"},
		xiaotou10_4 = {"xiaotou11"},
	},
	npc_dota_hero_puck = {
		shushi_tower5 = {"shushi1_3"},
		shushi_tower6 = {"shushi2_3"},
		shushi_tower7 = {"shushi3_3"},
		shushi_tower8 = {"shushi4_3"},
		shushi_tower9 = {"shushi5_3"},
		shushi_tower10 = {"shushi6_3"},
		shushi_tower11 = {"shushi9_3"},
		shushi7_3 = {"shushi11"},
		shushi7_4 = {"shushi11"},
		shushi8_3 = {"shushi11"},
		shushi8_4 = {"shushi11"},
		shushi10_3 = {"shushi11"},
		shushi10_4 = {"shushi11"},
	},
}

local heroList = {}

-- local lvlups = {
-- 	youxia1 = "youxia1_2",
-- 	youxia1_2 = "youxia1_3",
-- 	youxia2 = "youxia2_2",
-- 	youxia2_2 = "youxia2_3",
-- 	youxia3 = "youxia3_2",
-- 	youxia3_2 = "youxia3_3",
-- 	youxia4 = "youxia4_2",
-- 	youxia4_2 = "youxia4_3",
-- 	youxia5 = "youxia5_2",
-- 	youxia5_2 = "youxia5_3",
-- 	youxia6 = "youxia6_2",
-- 	youxia6_2 = "youxia6_3",
-- 	youxia7 = "youxia7_2",
-- 	youxia7_2 = "youxia7_3",
-- 	youxia8 = "youxia8_2",
-- 	youxia8_2 = "youxia8_3",
-- 	youxia9 = "youxia9_2",
-- 	youxia9_2 = "youxia9_3",
-- 	youxia10 = "youxia10_2",
-- 	youxia10_2 = "youxia10_3",
-- 	youxia12 = "youxia12_2",
-- 	youxia12_2 = "youxia12_3",
-- 	youxia13 = "youxia13_2",
-- 	youxia13_2 = "youxia13_3",
-- 	youxia14 = "youxia14_2",
-- 	youxia14_2 = "youxia14_3",
-- }
local max_limits = {
	yemanren_tower8 = 1,
}
local limit_record = {{},{},{},{}}
local globalState = {{},{},{},{}}

function M.checkLimits( playerId,abName )
	if max_limits[abName] and limit_record[playerId+1][abName] >= max_limits[abName] then
		return false
	else
		return true
	end
end

function M.updateAbilityState( playerId )
	local idx = playerId + 1
	local curUnits = GameRules.allUnits[idx]
	local heroName = PlayerResource:GetSelectedHeroName(playerId)
	local mytree = techTree[heroName]
	for entidx,hero in pairs(curUnits) do
		local abc = hero:GetAbilityCount()
		for i=0,abc-1 do
			local ab = hero:GetAbilityByIndex(i)
			if ab then
				local abn = ab:GetAbilityName()
				local needs = mytree[abn]
				if needs then
					local canBuild = true
					for _,need in pairs(needs) do
						if globalState[idx][need] <= 0 then
							canBuild = false
							break
						end
					end
					if canBuild and M.checkLimits(playerId,abn) then
						ab:SetLevel(1)
					else
						ab:SetLevel(0)
					end
				else
					if not ab.forbidChangeLv then
						local lv = hero:GetLevel()
						local ablv = ab:GetMaxLevel()
						local truelylv = math.min(lv,ablv)
						ab:SetLevel(truelylv)
					end
				end
			end
		end
	end
end

function M.buildOne( build )
	--print("buildOne ",buildName,abName)
	local playerId = build:GetPlayerOwnerID()
	local ent = build:entindex()
	local idx = playerId + 1
	GameRules.allUnits[idx][ent] = build
	local buildName = build:GetUnitName()
	local abName = build.abName
	
	if globalState[idx][buildName] then
		globalState[idx][buildName] = globalState[idx][buildName] + 1
	end
	if limit_record[idx][abName] then
		limit_record[idx][abName] = limit_record[idx][abName] + 1
	end
	build:AddAbility("common_gx")
	M.updateAbilityState(playerId)
end

function M.destroyOne( build )
	--print("destroyOne ",buildName,abName)
	local ent = build:entindex()
	local playerId = build:GetPlayerOwnerID()
	local idx = playerId + 1
	GameRules.allUnits[idx][ent] = nil
	local buildName = build:GetUnitName()
	local abName = build.abName
	
	if globalState[idx][buildName] then
		globalState[idx][buildName] = globalState[idx][buildName] - 1
	end
	if limit_record[idx][abName] then
		limit_record[idx][abName] = limit_record[idx][abName] - 1
	end
	M.updateAbilityState(playerId)
end

function M.initPlayerState( hero )
	local heroName = hero:GetUnitName()
	local playerId = hero:GetPlayerOwnerID()
	local idx = playerId + 1
	GameRules.allUnits[idx][hero:entindex()] = hero
	for k,v in pairs(techTree[heroName]) do
		for abn,need in pairs(v) do
			globalState[idx][need] = 0
		end
	end
	for k,v in pairs(max_limits) do
		limit_record[idx][k] = 0
	end
	M.updateAbilityState(playerId)
	--在这里不能使用GetSelectedHero，神之bug
end

function M.saveHero( id,hero )
	heroList[id] = hero
end

function M.getHeroById( id )
	return heroList[id]
end

function M.autoLevelup( unit )
	
end

function M.getState( hero,abilityName )
	local ret = {}
	local playerId = hero:GetPlayerOwnerID()
	local heroName = hero:GetUnitName()
	local needs = techTree[heroName][abilityName] or {}
	local a = {}
	local idx = playerId + 1
	for i,v in ipairs(needs) do
		a[v] = globalState[idx][v]
	end

	ret.curstate = a
	ret.max_limits = max_limits[abilityName]
	ret.limit_record = limit_record[idx][abilityName]
	return ret
end
print("techTree inited")
return M